#include <REGX52.H>
#include "I2C.h"

#define AT24C02_ADDRESS		0xA0	//AT24C02地址

 /**
  * 函    数：AT24C02写入一个字节
  * 参    数：WordAddress 要写入字节的地址
		      Data 要写入的数据
  * 返 回 值：无
  */
void AT24C02_WriteByte(unsigned char WordAddress,Data)
{
	I2C_Start();					//I2C开始
	I2C_SendByte(AT24C02_ADDRESS);	//发送AT24C02地址
	I2C_ReceiveAck();				//接收应答
	I2C_SendByte(WordAddress);		//发送要写入字节的地址
	I2C_ReceiveAck();				//接收应答
	I2C_SendByte(Data);				//发送数据
	I2C_ReceiveAck();				//接收应答
	I2C_Stop();						//I2C停止
}

/**
 * 函    数：AT24C02读取一个字节
 * 参    数：WordAddress 要读出字节的地址
 * 返 回 值：无
 */
unsigned char AT24C02_ReadByte(unsigned char WordAddress)
{
	unsigned char Data;
	I2C_Start();						//I2C开始
	I2C_SendByte(AT24C02_ADDRESS);		//发送AT24C02地址
	I2C_ReceiveAck();					//接收应答
	I2C_SendByte(WordAddress);			//发送要读出字节的地址
	I2C_ReceiveAck();					//接收应答
	I2C_Start();						//I2C开始
	I2C_SendByte(AT24C02_ADDRESS|0x01);	//发送读数据的请求
	I2C_ReceiveAck();					//接收应答
	Data=I2C_ReceiveByte();				//接收读取的数据
	I2C_SendAck(1);						//发送应答
	I2C_Stop();							//停止I2C
	return Data;
}
